package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.CureRecordFileLogic;
import cn.topevery.garden.dal.logic.CureRecordLogic;
import cn.topevery.garden.entity.dto.CureRecordDTO;
import cn.topevery.garden.entity.po.CureRecord;
import cn.topevery.garden.entity.po.CureRecordFile;
import cn.topevery.garden.entity.query.CureRecordQuery;
import cn.topevery.garden.entity.vo.CureRecordVO;
import com.github.dozermapper.core.Mapper;
import com.github.dozermapper.core.MappingException;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 防治记录
 *
 * @author code-generate
 * @date 2022-02-18
 */
@Api(tags = "防治记录")
@RestController
@RequestMapping("/cureRecord")
public class CureRecordController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    CureRecordLogic cureRecordLogic;

    @Autowired
    CureRecordFileLogic cureRecordFileLogic;

    /**
     * 获取防治记录分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取防治记录页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<CureRecordVO>> listPage(@RequestBody CureRecordQuery query) {
        DataPager<CureRecordVO> page = cureRecordLogic.listPage(query);
        for (CureRecordVO cureRecordVO : page.records) {
            List<CureRecordFile> list = cureRecordFileLogic.getListByMainId(cureRecordVO.getId());
            cureRecordVO.setFileList(list);
        }
        return JsonResult.ok(page);
    }

    @ApiOperation("新增/编辑 防治记录信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody CureRecordDTO dto) {
        CureRecord cureRecord = mapper.map(dto, CureRecord.class);
        if (cureRecord.getId() == null) {
            cureRecord.setDbStatus(false);
        }
        String userId = getUser().id.toString();
        try {
            cureRecordLogic.saveOrUpdate(userId, cureRecord);
            cureRecordFileLogic.saveOrUpdate(userId, cureRecord.getId(), cureRecord.getFileList());
            return JsonResult.ok(true);
        } catch (MappingException e) {
            e.printStackTrace();
            return JsonResult.ok(false);
        }
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<CureRecordVO> getInfoById(@RequestParam Integer id) {
        CureRecord entity = cureRecordLogic.getEntity(id);
        CureRecordVO vo= mapper.map(entity, CureRecordVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        cureRecordLogic.deleteBatchById(getUser().id.toString(),ids);
        cureRecordFileLogic.deleteByMainIds(ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出防治记录信息")
    @GetMapping("/export")
    public void export(CureRecordQuery query, HttpServletResponse response) {
        List<CureRecordVO> list = cureRecordLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "防治记录", "sheet1", CureRecordVO.class, "防治记录", response);
    }

}
